package L623;

import java.util.Stack;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-06-24 19:27
 * @version: 1.0
 */
public class Offer31 {
    //pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if (pushed.length == 0 || popped.length == 0) return true;
        Stack<Integer> stack = new Stack<>();
        int i=0;
        for(int p:pushed){
            stack.push(p);
            while (!stack.isEmpty()&&stack.peek()==popped[i]){
                stack.pop();
                i++;
            }
        }
        return stack.isEmpty();
    }
}
